#! /bin/sh

#  Copyright (c) International Business Machines  Corp., 2002
#
#  This program is free software;  you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#  the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program;  if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#  02/05/03  Modified - Manoj Iyer <manjo@mail.utexas.edu> use USCTEST macros
#            fixed bugs.
#  07/26/05  Michael Reed  <mreedltp@vnet.ibm.com>
#            Made changes to account for the replacement of syslogd 
#            with syslog-ng
#
##################################################################
# case1: Test whether messages are logged to the specified file  #
#        in the configuration file.                              #
#                                                                #
#        Send messages to syslogd at some level and facility     #
#        and grep for those messages.                            #
#                                                                #
# syslog.conf should contain:                                    #
#        *.crit           /usr/adm/critical                      #
#         mail.info        /usr/spool/adm/syslog                 #
##################################################################

#number of seconds to wait for another syslog test to complete
wait_count=60

#configuration file for syslog or syslog-ng
CONFIG_FILE=""

#Command for syslog or syslog-ng
syslog_cmd=""

cleanup()
{
  if [ $status_flag -eq 0 ]
  then
	tst_resm TPASS "success in generating messages"
  else
	tst_resm TFAIL "failed to generate messages"
 fi

#Restore syslog.conf
  [ -f "$CONFIG_FILE.ltpback" ] && mv "$CONFIG_FILE.ltpback" "$CONFIG_FILE"

#Restart syslog
  [ -x "$syslog_cmd" ] && {
      tst_resm TINFO "restart $syslog_cmd"
      "$syslog_cmd" restart 2>&1 1>/dev/null
  }

 

 sleep 2

exit $status_flag
}

syslog_case1()
{


	
	RC=0
	RSYSLOG_CONFIG=""
	#set the trap to handle signals.
	trap '
		tst_resm TBROK "Testing is terminating due to a signal"
		status_flag=1
		cleanup
	' 1 2 3 6 11 15

	tst_resm TINFO  "syslog: Testing whether messages are logged into log file"
	echo
	
#check to see if syslogd or syslog-ng exists
	if [ -e /sbin/syslogd  ]
	then
	    CONFIG_FILE="/etc/syslog.conf"
	    syslog_cmd="/etc/init.d/sysklogd"
	elif [ -e /sbin/syslog-ng ] || [ -e /usr/sbin/syslog-ng ]
        then
	    CONFIG_FILE="/etc/syslog-ng/syslog-ng.conf"
	    syslog_cmd="/etc/init.d/syslog-ng"
	elif [ -e /sbin/rsyslogd ] || [ -e /usr/sbin/rsyslogd ]
        then
	    CONFIG_FILE="/etc/rsyslog.conf"
	    syslog_cmd="/etc/init.d/rsyslog"
	    RSYSLOG_CONFIG='$ModLoad imuxsock.so'
	else
	    tst_resm TCONF "syslogd, syslog-ng or rsyslogd no such command"
	    status_flag=0
	    cleanup	
	fi
	[ -x $syslog_cmd ] || syslog_cmd="/etc/init.d/syslog"

#Back up configuration file
        if [ ! -e $CONFIG_FILE ]
        then
	   tst_resm TBROK  "$CONFIG_FILE not found!"
        else
#Pause if another LTP syslog test is running
           while [ -e $CONFIG_FILE.ltpback ]  
             do
               if [ $(( wait_count-- )) -gt 0 ]
               then
                 sleep 1
               else
	        tst_resm TBROK "Another syslog test appears to be \
                                stuck, could not run"
                status_flag=1
	         exit $status_flag
               fi
             done
           cp $CONFIG_FILE $CONFIG_FILE.ltpback
          fi

         echo "Running tests"

	# check if /etc/init/syslog script exists
	if ! [ -e "$syslog_cmd" ]
	then
		tst_resm TBROK "/etc/init.d/syslog no such script"
		status_flag=1
                cleanup
	fi



	# Create the configuration file specific to this test case.

	case "$CONFIG_FILE" in
			"/etc/syslog.conf" |\
			"/etc/rsyslog.conf")
					echo "$RSYSLOG_CONFIG" > $CONFIG_FILE
					echo "*.crit		/var/log/messages" >>  $CONFIG_FILE
					echo "mail.info	/var/log/maillog" >>  $CONFIG_FILE;;

			"/etc/syslog-ng/syslog-ng.conf")
					echo "source src{ internal(); unix-dgram(\"/dev/log\"); \
						udp(ip(\"0.0.0.0\") port(514)); };" > $CONFIG_FILE
					echo " " >> $CONFIG_FILE
					echo " " >> $CONFIG_FILE
					echo "# Added for syslog testcase"  >> $CONFIG_FILE
					echo "filter f_syslog     { level(crit);};" >> $CONFIG_FILE
					echo "filter f_syslogMail { level(info)      and facility(mail); };"\
						>> $CONFIG_FILE
					echo "destination syslog-messages { file(\"/var/log/messages\");};"\
						>> $CONFIG_FILE
					echo "log { source(src); filter(f_syslog); destination(syslog-messages);\
						};" >> $CONFIG_FILE
					echo "destination syslog-mail { file(\"/var/log/maillog\");};" \
						>> $CONFIG_FILE
					echo "log { source(src); filter(f_syslogMail); destination(syslog-mail);\
						};"  >> $CONFIG_FILE;;
	esac

       #Restart syslog
       $syslog_cmd restart 2>&1 1>/dev/null
	sleep 2
	

	# check if /var/log/maillog script exists
	if [ "$CONFIG_FILE" != "/etc/syslog-ng/syslog-ng.conf" ] && ! [ -e /var/log/maillog ]
	then
		tst_resm TBROK "/var/log/maillog no such log file"
		status_flag=1
                cleanup
	fi

	# Grepping pattern has to be changed whenever the executable name
	# changes, ex: syslogtst executable.
        # This check is neccessary for syslog-ng because /var/log/maillog is
        # only created after syslogtst
        if [ -e /var/log/maillog ]; then
	    oldvalue1=`grep -c "syslogtst: mail info test" /var/log/maillog`
	else
	    oldvalue1=0
	fi

	# Call syslogtst executable with case number as argument
	syslogtst 1 2>/dev/null
	if [ $? -ne 0 ]; then
		status_flag=1
		return
	fi
	sleep 2

	newvalue1=`grep -c "syslogtst: mail info test" /var/log/maillog`
	diff=$(( $newvalue1 - $oldvalue1 ))
	if [ $diff -ne 1 ]; then
		status_flag=1
	fi
}

export TST_TOTAL=1
export TST_COUNT=1
export TCID=syslog01

tst_resm TINFO "Send messages to syslogd at some level "
tst_resm TINFO "and facility and grep for those messages."

status_flag=0


syslog_case1
cleanup
